<?php

class Gmapsprofile_View_Helper_GmapsprofileFields extends Zend_View_Helper_Abstract {
	protected $_requireProfileType = true;
	public function gmapsprofileFields($subject, $_fieldType = 'user') {
		$user = $subject;
		$gender = '';
		$birthdate = '';
		$settings = Engine_Api::_ ()->getApi ( 'settings', 'core' );
		// Set data
		$mapData = Engine_Api::_ ()->getApi ( 'core', 'fields' )->getFieldsMaps ( $_fieldType );
		$metaData = Engine_Api::_ ()->getApi ( 'core', 'fields' )->getFieldsMeta ( $_fieldType );
		$optionsData = Engine_Api::_ ()->getApi ( 'core', 'fields' )->getFieldsOptions ( $_fieldType );
		$valueData = Engine_Api::_ ()->fields ()->getFieldsValues ( $user );
		$profile_type = Engine_Api::_ ()->fields ()->getFieldsSearch ( $user );
		
		if ($profile_type->profile_type == "")
			return $profile_type->profile_type;
		
		// Get top level fields
		$topLevelMaps = $mapData->getRowsMatching ( array ('field_id' => 0, 'option_id' => 0 ) );
		$topLevelFields = array ();
		foreach ( $topLevelMaps as $map ) {
			$field = $map->getChild ();
			$topLevelFields [$field->field_id] = $field;
		}
		
		// Do we require profile type?
		// No
		if (! $this->_requireProfileType) {
			$this->topLevelOptionId = '0';
			$this->topLevelFieldId = '0';
		} // Yes
		else {
			// Get top level field
			// Only allow one top level field
			if (count ( $topLevelFields ) > 1) {
				throw new Engine_Exception ( 'Only one top level field is currently allowed' );
			}
			$topLevelField = array_shift ( $topLevelFields );
			// Only allow the "profile_type" field to be a top level field (for now)
			if ($topLevelField->type !== 'profile_type') {
				throw new Engine_Exception ( 'Only profile_type can be a top level field' );
			}
			
			// Get top level options
			$topLevelOptions = array ();
			foreach ( $optionsData->getRowsMatching ( 'field_id', $topLevelField->field_id ) as $option ) {
				$topLevelOptions [$option->option_id] = $option->label;
			}
			$this->view->topLevelOptions = $topLevelOptions;
			
			// Get selected option
			

			$option_id = $profile_type->profile_type;
			if (empty ( $option_id ) || empty ( $topLevelOptions [$option_id] )) {
				$option_id = current ( array_keys ( $topLevelOptions ) );
			}
			$topLevelOption = $optionsData->getRowMatching ( 'option_id', $option_id );
			
			if (! $topLevelOption) {
				throw new Engine_Exception ( 'Missing option' );
			}
			$this->view->topLevelOption = $topLevelOption;
			$this->view->topLevelOptionId = $topLevelOption->option_id;
			$level = 'gmapsprofile_level_' . $option_id;
			$fields_address = isset ( $settings->$level ) ? explode ( ';', $settings->$level ) : '';
			// Get second level fields
			$secondLevelMaps = array ();
			if (! empty ( $option_id )) {
				$secondLevelMaps = $mapData->getRowsMatching ( 'option_id', $option_id );
				if (! empty ( $secondLevelMaps )) {
					$prefix = '';
					$location = '';
					foreach ( $secondLevelMaps as $map ) {
						$oField = $map->getChild ();
						
						if ($oField && $oField->display) {
							if (! empty ( $settings->$level ) && in_array ( $oField->field_id, $fields_address )) {
								$Data = $valueData->getRowMatching ( 'field_id', $oField->field_id );
								$location .= ! empty ( $Data->value ) ? $prefix . $Data->value : '';
								$prefix = ', ';
							} elseif (empty ( $settings->$level )) {
								switch ($oField->type) {
									case 'location' :
										$Data = $valueData->getRowMatching ( 'field_id', $oField->field_id );
										$location .= ! empty ( $Data->value ) ? $prefix . $Data->value : '';
										$prefix = ', ';
										break;
									case 'city' :
										$Data = $valueData->getRowMatching ( 'field_id', $oField->field_id );
										$location .= ! empty ( $Data->value ) ? $prefix . $Data->value : '';
										$prefix = ', ';
										break;
									case 'country' :
										$Data = $valueData->getRowMatching ( 'field_id', $oField->field_id );
										$location .= ! empty ( $Data->value ) ? $prefix . $Data->value : '';
										$prefix = ', ';
										break;
									case 'zip_code' :
										$Data = $valueData->getRowMatching ( 'field_id', $oField->field_id );
										$location .= ! empty ( $Data->value ) ? $prefix . $Data->value : '';
										$prefix = ', ';
										break;
								}
							}
							switch ($oField->type) {
								case 'gender' :
									$Data = $valueData->getRowMatching ( 'field_id', $oField->field_id );
									$value = $optionsData->getRowMatching ( 'option_id', $Data->value );
									$gender = ! empty ( $value->label ) ? $value->label : '';
									break;
								case 'birthdate' :
									$Data = $valueData->getRowMatching ( 'field_id', $oField->field_id );
									$birthdate = ! empty ( $Data->value ) ? $Data->value : '';
									if (! empty ( $birthdate )) {
										$parts = @explode ( '-', $birthdate );
										
										// Error if not filled out
										if (count ( $parts ) < 3 || count ( array_filter ( $parts ) ) < 3) {
											//$this->addError('Please fill in your birthday.');
											$birthdate = '';
										} else {
											$value = mktime ( 0, 0, 0, $parts [1], $parts [2], $parts [0] );
											
											// Error if too low
											$date = new Zend_Date ( $value );
											$birthdate = ( int ) (- $date->sub ( time () ) / 365 / 86400) . Zend_Registry::get ( 'Zend_Translate' )->_ ( ' y/o' );
										}
									}
									break;
							}
						}
					}
				}
			}
		}
		return array ('location' => $location, 'gender' => $gender, 'birthdate' => $birthdate );
	}
}